package definition;

import domain.User;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 直接注册外部对象
 * @author jackcfan
 * @date 2021/9/12
 */
public class SingletonBeanRegistrationTest {
    @Test
    public void test(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        // 创建一个外部对象
        User user = User.createUser();

        // 注册一个外部对象 Bean
        applicationContext.getBeanFactory().registerSingleton("outerUser", user);
        applicationContext.refresh();

        // 从容器获取对象
        // 查找时，先从当立bean中查找
        User userBean = applicationContext.getBean("outerUser", User.class);
        System.out.println("user == userBean ? "+(user == userBean));

    }
}
